/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.wizards;

import it.cums.controller.ApplicationController;
import it.cums.model.Suppliers;
import it.cums.util.SupplierManager;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;

public class SupplierWizard extends Wizard implements IWorkbenchWizard {
	private SupplierBasicWizardPage supplierBasicWizardPage;
	private SupplierPersonalWizardPage supplierPersonalWizardPage;
	private SupplierAccountWizardPage supplierAccountWizardPage;
	private SupplierNotesWizardPage supplierNotesWizardPage;
	private ApplicationController appController = ApplicationController.getInstance();
	
	public SupplierWizard() {
		super();
		setNeedsProgressMonitor(true);
		setWindowTitle("New Supplier");
	}

	@Override
	public void addPages() {
		// TODO Auto-generated method stub
		supplierBasicWizardPage = new SupplierBasicWizardPage();
		addPage(supplierBasicWizardPage);
		supplierPersonalWizardPage = new SupplierPersonalWizardPage();
		addPage(supplierPersonalWizardPage);
		supplierAccountWizardPage = new SupplierAccountWizardPage();
		addPage(supplierAccountWizardPage);
		supplierNotesWizardPage = new SupplierNotesWizardPage();
		addPage(supplierNotesWizardPage);
	}
	
	@Override
	public boolean performFinish() {
		// TODO Auto-generated method stub
		Suppliers suppliers = appController.getSuppliers();
		try {
			SupplierManager.getInstance().createAndStoreSupplier(suppliers, supplierBasicWizardPage.getName(), supplierBasicWizardPage.getReferencePerson(), supplierBasicWizardPage.getTaxNumber(), 
										  supplierPersonalWizardPage.getAddress(), supplierPersonalWizardPage.getCAP(), supplierPersonalWizardPage.getLocation(), supplierPersonalWizardPage.getCountry(), 
										  supplierPersonalWizardPage.getPhone(), supplierPersonalWizardPage.getMobilePhone(), supplierPersonalWizardPage.getEmail(), supplierPersonalWizardPage.getWebSite(), 
										  supplierAccountWizardPage.getAccountDetails(), supplierNotesWizardPage.getNotes());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public void init(IWorkbench arg0, IStructuredSelection arg1) {
	}
}
